package com.tumblr.logico2012.screens;

import processing.core.PApplet;


public class PopupScreen {

	  public final static float STD_BLURRING_TIME = 1.5f; //seconds
	
	  
	  private int POPUP_TIME = 5; //seconds
	  private int CENTER_X = 0; // x position of popup
	  private int CENTER_Y = 0; // y position of popup
	  private int WIDTH = 400;	
	  private int HEIGHT = 200;
	  private float blur_effect = 0.1f; // percentage 
	  private float blur_time;
	  private boolean IS_ACTIVE = false;
	  private int ID;
	  
	  private int counter = 0;
	  
	  public int start_s=0; // starttime seconds
	  public int start_m=0; // starttime minutes
	  
	  private PApplet p;
	  private String m_txt;
	  private boolean grow = true;
	  
	  public PopupScreen(PApplet processor, int _id, String txt) {
		    p = processor;
		    ID = _id;
		    m_txt = txt;
		    blur_time = STD_BLURRING_TIME;
		    CENTER_X = p.width/2;
		    CENTER_Y = p.height/2;
	  }
	  
	  public PopupScreen(PApplet processor, int _id, String message, int _blur_time, float _blur_effect) {
		    p = processor;
		    m_txt = message;
		    blur_time = _blur_time;
		    blur_effect = _blur_effect;
		    CENTER_X = p.width/2;
		    CENTER_Y = p.height/2;
		  }
	  	
	  public void display() {  
		  
		  float duration = blur_time * (int)p.frameRate;
		  float blur_factor = (1 + blur_effect*counter/duration);
		  
		  //check blurring out or in
		  if (grow)
		    if (counter < duration) counter++;
		    else { grow = false; counter++;}
		  else if (!grow)
		    if (counter > 0) counter--;
		    else { grow = true; counter--;}
		  
		   //display ellipse
		  p.noStroke();
		  p.fill(255,234);
		  p.ellipse(CENTER_X,CENTER_Y, WIDTH*blur_factor, HEIGHT*blur_factor);
		  
		  //display message
		  p.textAlign(PApplet.CENTER, PApplet.CENTER);
		  float tz = 1.3f; //textzoom
		  p.textSize(HEIGHT/7*(blur_factor * tz - tz + 1));
		  p.fill(1,234);
		
		  p.text(m_txt, CENTER_X-.75f*WIDTH/2*blur_factor, CENTER_Y-.75f*HEIGHT/2*blur_factor, .75f*WIDTH*blur_factor, .7f*HEIGHT*blur_factor);
		  p.textAlign(PApplet.LEFT);
				
	  }
	  
	  
	  public boolean isActive() {
		  if (IS_ACTIVE) {
			  if (start_m == PApplet.minute()) {
				  if (!(PApplet.second() - start_s < POPUP_TIME)) this.setInactive();
			  }
			  else if (start_m + 1 == PApplet.minute())
			  {
				  if (!(PApplet.second() + 60 - start_s < POPUP_TIME)) this.setInactive();
			  }
			  else this.setInactive();
		  }
		  else this.setInactive();;
		  
		  return IS_ACTIVE;
	  }
	 
	  // activate popup
	  public void setActive() {
		  this.IS_ACTIVE = true;
		  this.start_s = PApplet.second();
		  this.start_m = PApplet.minute();
	  }
	  
	  public void setInactive() {
		  this.IS_ACTIVE = false;
	  }
	  
	  public void setPosition(int x, int y) {
		  this.CENTER_X = x;
		  this.CENTER_Y = y;
	  }
	  
	  public void setSize(int _width, int _height) {
		  this.WIDTH = _width;
		  this.HEIGHT = _height;
	  }

	  public void setPopupTime(int t) {
		  this.POPUP_TIME = t;
	  }

	  public int getId() { return ID; }
	  
	  
}
